<template>
    <view class="center page">
        <picker
            @change="cityLiseChange"
            v-if="cityLise.length>0"
            :range="cityLise"
            range-key="name"
        >
            <div class="listItm">
                <div class="listFlex main">
                    <div class="listLeft">请选择所在城市</div>
                    <div class="listRight">
                        <span class="listCheckT">{{from.city}}</span>
                        <span class="weui-cell__ft_in-access"></span>
                    </div>
                </div>
            </div>
        </picker>
        <picker @change="hospitalLiseChange" :range="hospitalLise" range-key="name">
            <div class="listItm">
                <div class="listFlex main">
                    <div class="listLeft">请选择医院</div>
                    <div class="listRight">
                        <span class="listCheckT">{{from.hospital}}</span>
                        <span class="weui-cell__ft_in-access"></span>
                    </div>
                </div>
            </div>
        </picker>
        <!--<button  @click="getCode">二维码</button>-->
        <div class="btn">
            <div class="saveBtn" @click="saveInf()">
                <span>选好了</span>
            </div>
        </div>
    </view>
</template>

<script>
import "@/css/weui.css";
export default {
    data() {
        return {
            from: {
                city: "",
                hospital: ""
            },
            url64:'',
            city: 0,
            hospital: 0,
            cityLise: [],
            hospitalLise: []
        };
    },
    async onPullDownRefresh() {
        // to doing..
        this.lodingtop();
        // 停止下拉刷新
    },
    onShow() {
        uni.getStorage({
            key: "token",
            success: function(res) {
                if (res.data) {
                    uni.reLaunch({
                        url: "../index/index"
                    });
                }
            }
        });
        var _this = this;
        uni.getStorage({
            key: "hospital",
            success: function(res) {
                if (res.data) {
                    _this.from.city = res.data.city;
                    _this.from.hospital = res.data.name;
                    _this.$ajaxs
                        .get("/mobile/v1/hospital/getHospital", {
                            city: _this.from.city
                        })
                        .then(res => {
                            _this.hospitalLise = res.content;
                            for (
                                let i = 0;
                                i < _this.hospitalLise.length;
                                i++
                            ) {
                                const element = _this.hospitalLise[i];
                                if (element.name == _this.from.hospital) {
                                    _this.hospital = i;
                                }
                            }
                        });
                }
            }
        });
    },
    mounted() {},
    onLoad() {
        this.getAreaList();
    },
    methods: {
        getCode() {
            var _this = this;
            var row = _this.hospitalLise[_this.hospital]
            var access_token = "";
            var url =
                "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx4cc8c6119f115283&secret=e1febb5128817354dcb2dc6c53993177";
            var posturl =
                "https://api.weixin.qq.com/wxa/getwxacode?access_token=";
            var data = {
                path:
                    "pages/login/index?id=" +
                    row.id +
                    "&name=" +
                    row.name +
                    "&gatewayAddress=" +
                    row.gatewayAddress,
                width: "450"
            };
            uni.request({
                url: url,
                success: res => {
                    access_token = res.data.access_token;
                    uni.request({
                        url: posturl + access_token,
                        method: "POST",
                        data: data,
                        success: res => {
                           
                        }
                    });
                }
            });
        },
        lodingtop() {
            this.getAreaList();
            uni.stopPullDownRefresh();
        },
        getAreaList() {
            var _this = this;
            _this.$ajaxs
                .get("/mobile/v1/area/getList", {})
                .then(res => {
                    let array = res.content;
                    var data = [];
                    for (let index = 0; index < array.length; index++) {
                        const element = array[index].children;
                        for (
                            let index1 = 0;
                            index1 < element.length;
                            index1++
                        ) {
                            const elementT = element[index1];
                            data.push(elementT);
                        }
                    }
                    _this.cityLise = data;
                })
                .catch(err => reject(err));
        },
        saveInf() {
            var _this = this;
            if (!_this.from.city) {
                return uni.showToast({
                    title: "请选择所在城市！",
                    icon: "none"
                });
            }
            if (!_this.from.hospital) {
                return uni.showToast({
                    title: "请选择医院！",
                    icon: "none"
                });
            }
            uni.setStorage({
                //存储到本地
                key: "hospital",
                data: _this.hospitalLise[_this.hospital]
            });
            uni.navigateTo({
                url: "index",
                animationType: "pop-in",
                animationDuration: 200
            });
        },
        cityLiseChange(e) {
            var _this = this;
            _this.city = e.target.value;
            _this.from.city = _this.cityLise[_this.city].name;
            _this.$ajaxs
                .get("/mobile/v1/hospital/getHospital", {
                    city: _this.cityLise[_this.city].name
                })
                .then(res => {
                    _this.hospitalLise = res.content;
                    _this.hospital = 0;
                })
                .catch(err => reject(err));
        },
        hospitalLiseChange(e) {
            var _this = this;
            _this.hospital = e.target.value;
            _this.from.hospital = _this.hospitalLise[_this.hospital].name;
        }
    }
};
</script>

<style scoped>
/* page {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    min-height: 100%;
    height: auto;
} */
.listItm {
    font-size: 28upx;
    height: 100upx;
    line-height: 100upx;
    border-bottom: 1px solid #ddd;
}
.listFlex {
    display: flex;
    height: 100%;
    justify-content: space-between;
}
.listCheckT {
    color: #999;
    margin-right: 16upx;
}
.listRight {
    width: 60%;
    text-align: right;
}
.listLeft {
    width: 40%;
}
.inputStyle {
    display: inline-block;
    padding-top: 24upx;
    text-align: right;
    color: #999;
}
.hiddenCom {
    visibility: hidden;
}
.btmCon {
    height: 180upx;
    margin-top: 20upx !important;
    background: #fff;
    color: #fa4d93;
}
.btn {
    position: fixed;
    bottom: 0px;
    left: 0px;
    margin-top: 80pux;
    width: 100%;
    height: 128upx;
    background: #fff;
    display: flex;
    justify-content: space-between;
}
.saveBtn {
    height: 80upx;
    width: 100%;
    text-align: center;
    line-height: 80upx;
    border: 1px solid #fa4d93;
    border-radius: 40upx;
    color: #fa4d93;
    font-size: 32upx;
    background: #fff;
    margin: 0 40upx;
}
</style>